home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / MatrixEnumeration.java < prev    next >
Text File  |  1998-08-21  |  5KB  |  221 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.util.Enumeration;
  5. import java.text.MessageFormat;
  6. import java.util.ResourceBundle;
  7.  
  8.  
  9. //     01/30/97    RKM    Integrated fix to hasMoreElements from flash@tansu.slab.tnr.sharp.co.jp (George Rome Borden IV)
  10. //     03/27/97    RKM    Made class public, so peole could use it
  11. //                    Made class final
  12. //                    Tried to make some sense of protection on methods & members
  13. //     04/17/97    LAB Added Don Haney's (haney@tiac.net) fix that addressed a NullPointerException in hasMoreElements().
  14.  
  15. /**
  16.  * This class is used to enumerate Matrix elements. Enumeration is used to
  17.  * sequentially scan through all the elements in a Matrix one at a time.
  18.  * @see symantec.itools.awt.Matrix
  19.  * @see java.util.Enumeration
  20.  *
  21.  * @version 1.0, Nov 26, 1996
  22.  *
  23.  * @author    Symantec
  24.  *
  25.  */
  26.  
  27. public final class MatrixEnumeration
  28.     implements Enumeration
  29. {
  30.     //--------------------------------------------------
  31.     // public constructors
  32.     //--------------------------------------------------
  33.  
  34.     /**
  35.      * Constructs a matrix enumerator for the given Matrix.
  36.      */
  37.     public MatrixEnumeration(Matrix matrix)
  38.     {
  39.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  40.         m = matrix;
  41.         started = false;
  42.     }
  43.  
  44.     //--------------------------------------------------
  45.     // public members
  46.     //--------------------------------------------------
  47.  
  48.     /**
  49.      * Returns true if more elements are available to enumerate.
  50.      * This is a standard Java Enumeration interface method.
  51.      * @return true if more elements available, false if done enumerating
  52.      */
  53.     public boolean hasMoreElements()
  54.     {
  55.         if (m == null)
  56.             return false;
  57.         if (started == false && m.o != null) return true;
  58.         if (m.nextElt != null) return true;
  59.         return hasMoreRows();
  60.     }
  61.  
  62.     /**
  63.      * Returns true if more rows are available to enumerate.
  64.      */
  65.     public boolean hasMoreRows()
  66.     {
  67.         if (m.nextRow == null)
  68.         {
  69.             return false;
  70.         }
  71.  
  72.         Matrix n = m.nextRow;
  73.  
  74.         //find next row
  75.         while(n.o == null && n.nextElt == null)
  76.         {
  77.             if ((n = n.nextRow) == null)  return false;
  78.         }
  79.  
  80.         return true;
  81.     }
  82.  
  83.     /**
  84.      * Returns the next element.
  85.      * This is a standard Java Enumeration interface method.
  86.      */
  87.     public Object nextElement()
  88.     {
  89.         if (!started)
  90.         {
  91.             started = true;
  92.  
  93.             if (m.o != null)
  94.             {
  95.                 return m.o;
  96.             }
  97.         }
  98.  
  99.         if (m == null)
  100.         {
  101.             return null;
  102.         }
  103.  
  104.         if (m.nextElt != null)
  105.         {
  106.             m = m.nextElt;
  107.         } else {
  108.  
  109.                 if ((m = findNextRow()) == null)
  110.                 {
  111.                     return null;
  112.                 }
  113.                 }
  114.  
  115.         return m.o;
  116.     }
  117.  
  118.     /**
  119.      * Returns the row number of the current element.
  120.      */
  121.     public int currRow()
  122.     {
  123.         return m.row;
  124.     }
  125.  
  126.     /**
  127.      * Returns the column number of the current element.
  128.      */
  129.     public int currCol()
  130.     {
  131.         return m.col;
  132.     }
  133.  
  134.     /**
  135.      * Skips enumeration ahead to the start of the next row.
  136.      * @return the first element in the next row.
  137.      */
  138.     public Object nextRow()
  139.     {
  140.         m = findNextRow();
  141.  
  142.         return m.o;
  143.     }
  144.  
  145.     /**
  146.      * Skips enumeration ahead to the start of the given row.
  147.      * @param r the row to skip ahead to. Must be greater than the current row
  148.      * @return the first element in the given row
  149.      * @exception IllegalArgumentException if r is less than or equal to the
  150.      * current row
  151.      */
  152.     public Object advanceTo(int r)
  153.         throws IllegalArgumentException
  154.     {
  155.         started = true;
  156.  
  157.         if (r < m.row || r == m.row)
  158.         {
  159.             Object[] args = { new Integer(r), new Integer(m.row) };
  160.             throw new
  161.                IllegalArgumentException(MessageFormat.format(errors.getString("MustBeGreaterThanCurrentRow"), args));
  162.         }
  163.  
  164.         Matrix n = m;
  165.  
  166.         while (m.row < r)
  167.         {
  168.             m = findNextRow();
  169.  
  170.             if (m == null)
  171.             {
  172.                 m = n;
  173.                 Object[] args = { new Integer(r) };
  174.                 throw new
  175.                    IllegalArgumentException(MessageFormat.format(errors.getString("RowTooLarge"), args));
  176.             }
  177.         }
  178.  
  179.         return m.o;
  180.     }
  181.  
  182.     //--------------------------------------------------
  183.     // private members
  184.     //--------------------------------------------------
  185.  
  186.     private Matrix findNextRow()
  187.     {
  188.         started = true;
  189.         Matrix n = m.nextRow;
  190.  
  191.         if (n == null)
  192.         {
  193.             return null;
  194.         }
  195.  
  196.         //find next row
  197.         while(n.o == null && n.nextElt == null)
  198.         {
  199.             if ((n = n.nextRow) == null)
  200.             {
  201.                 return null;
  202.             }
  203.         }
  204.  
  205.         n = n.o != null  ?n  :n.nextElt;
  206.         return n;
  207.     }
  208.  
  209.     /**
  210.      * Error strings.
  211.      */
  212.     transient protected ResourceBundle errors;
  213.  
  214.     //--------------------------------------------------
  215.     // private members
  216.     //--------------------------------------------------
  217.  
  218.     private Matrix        m;
  219.     private boolean    started;
  220. }
  221.